import { message } from 'antd'
export default {
  namespace: 'cj',
  state: {
    jointImageSrc: null, // 标定用的图片
    imageJointParams: [], // 相机拼接的时候使用的参数
    configParams: [], // 相机设置的参数
    countWidthHeightImageSrc: null, // 相机识别宽高的时候用到的图片
    imageShowModalVisble: false,  // 弹框是否显示
    boardSize: [], // 计算的宽度和高度的像素值
    inputSize: [],  // 界面上保存的录入的尺寸
    showInfo: "", // 结算的结果信息
    imageCheckModalVisble: false, // 验证用的显示框是否展示
    checkSizeImageSrc: null, // 验证用的图像显示
    checkSize: [], // 验证的尺寸,宽,高分别是mm
    checkInfo: "", // 验证尺寸结果显示
    checkError: [], // 识别的误差值
  },
  effects: {
    *handleLineCamJointMsg({ payload }, { call, put }) {
      const { msg } = payload;
      if (msg.val == "updateJointParams") {
        yield put({
          type: "getImageJointParams"
        });

        yield put({
          type: 'getConfigParams'
        });
      } else if (msg.val == "updateJointImage") {
        yield put({
          type: 'updateJointImageSrc',
          payload: msg.jointImageSrc,
        })
      } else if (msg.val == "updateCountWidthHeightImage") {
        yield put({
          type: 'updateCountWidthHeightImage',
          payload: [msg.countWidthHeightImgSrc, msg.inputSize, msg.boardSize, msg.showInfo],
        })
      } else if (msg.val == "updateCheckSizeImageSrc") {
        yield put({
          type: 'updateCheckSizeImageSrc',
          payload: [msg.checkSizeImageSrc, msg.inputSize, msg.checkSize, msg.checkInfo, msg.checkError],
        })
      }
    },
    *closeCamZmcDoorAlarm({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_requset_close_cam_zmc_door_alarm', []);
      if (response !== "ok") {
        message.error("屏蔽失败" + response, 5);
      }else{
        message.success("屏蔽成功",3)
      }
    },

    *stopCamZmc({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'camZmc', 'web_request_stop_cam_zmc', []);
      if (response !== "ok") {
        message.error("停止失败" + response, 5);
      }
    },
    *resetCamZmc({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'camZmc', 'web_request_reset_cam_zmc', []);
      if (response !== "ok") {
        message.error("复位失败" + response, 5);
      }
    },

    *calcCheckError({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_request_calc_check_error', []);
      if (response !== "ok") {
        message.error(response, 3);
      }
    },

    *getEightImageForCount({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_request_get_eight_image_for_count', []);
      if (response !== "ok") {
        message.error(response, 3);
      }
    },

    *checkSizeOk({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_request_check_size_ok', []);
      if (response !== "ok") {
        message.error(response, 3);
      }
    },

    *checkSizeError({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_request_check_size_error', []);
      if (response !== "ok") {
        message.error(response, 3);
      }
    },

    *openCountBoardSizeDir({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_request_open_count_board_size_dir', []);
      if (response !== "ok") {
        message.error(response, 5);
      } else {
        message.success("目录打开成功!", 3)
      }
    },
    *saveTotalBoardSize({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_request_save_total_board_size', [payload]);
      if (response !== "ok") {
        message.error(response, 5);
      } else {
        message.success("保存成功!", 3)
      }
    },

    *checkMmToPix({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_reqeust_check_mm_to_pix', []);
      if (response !== "ok") {
        message.error(response, 3);
      }
    },

    *saveMmToPix({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_request_save_mm_to_pix', []);
      if (response !== "ok") {
        message.error("保存失败" + response, 5);
      } else {
        message.success("保存成功!", 3)
        yield put({
          type: "clearBoardSizeImageSrc"
        });
        yield put({
          type: 'getConfigParams'
        });
      }
    },
    *calcMmToPix({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_request_calc_mm_to_pix', []);
      if (response !== "ok") {
        message.error(response, 3);
      }
    },
    *totalBoardIsOk({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_request_total_board_is_ok', []);
      if (response !== "ok") {
        message.error(response, 3);
      }
    },
    *totalBoardIsError({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_request_total_board_is_error', []);
      if (response !== "ok") {
        message.error(response, 3);
      }
    },
    *countBoardSize({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_request_count_board_size', []);
      if (response !== "ok") {
        message.error(response, 3);
      }
    },

    *reGetImageForImageJoint({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_request_re_get_image_for_image_joint', []);
      if (response !== "ok") {
        message.error(response, 3);
      }
    },
    *getImageForImageJoint({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_request_get_image_for_image_joint', []);
      if (response !== "ok") {
        message.error(response, 3);
      }
    },
    *getMoveJointImage({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_request_get_move_joint_image', [payload]);
      if (response !== "ok") {
        message.error(response, 3);
      }
    },
    *getImageJointParams({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'db', 'web_request_get_image_joint_params', []);
      yield put({
        type: "updateImageJointParams",
        payload: response,
      });
    },
    // *testFunction({ payload }, { call, put }) {
    //   const response = yield call(window.eel.run_py, 'imageJoint', 'test_new_function', []);
    // },

    *getConfigParams({ payload }, { call, put }) {
      const response2 = yield call(window.eel.run_py, 'db', 'web_request_get_config_params', []);
      yield put({
        type: 'updateConfigParams',
        payload: response2
      })
    },

    *startCamZmc({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'camZmc', 'web_request_start_cam_zmc', []);
      if (response === "ok") {
        message.success("设备启动成功!")
      } else {
        message.error("设备启动失败," + response, 5)
      }
    },

    *openImageJointDir({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_request_open_image_joint_dir', []);
      if (response === "ok") {
        message.success("目录打开成功!")
      } else {
        message.error("目录打开失败," + response, 5)
      }
    },

    *saveConfigParams({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'ph', 'web_request_save_config_params', [payload]);
      if (response === "ok") {
        message.success("保存成功!")
      } else {
        message.error("保存失败," + response, 5)
      }
      yield put({
        type: "getConfigParams"
      });
    },
    *saveImageJointParams({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'imageJoint', 'web_request_save_image_joint_params', [payload]);
      if (response === "ok") {
        message.success("保存成功!")
      } else {
        message.error("保存失败," + response, 5)
      }
      yield put({
        type: "getImageJointParams"
      });
    },
  },
  reducers: {
    updateImageJointParams(state, action) {
      return {
        ...state,
        imageJointParams: action.payload,
      }
    },
    updateConfigParams(state, action) {
      return {
        ...state,
        configParams: action.payload,
      }
    },
    clearJointImageSrc(state, action) {
      return {
        ...state,
        jointImageSrc: null
      }
    },
    updateJointImageSrc(state, action) {
      return {
        ...state,
        jointImageSrc: 'data:image/jpeg;base64,' + action.payload,
      }
    },
    clearBoardSizeImageSrc(state, action) {
      return {
        ...state,
        countWidthHeightImageSrc: null,
        imageShowModalVisble: false,
        inputSize: [],
        boardSize: [],
        showInfo: "",
      };
    },
    clearBoardCheckImageSrc(state, action) {
      return {
        ...state,
        imageCheckModalVisble: false,
        checkSizeImageSrc: null,
        inputSize: [],
        checkSize: [],
        checkInfo: "",
        checkError: [],
      };
    },
    updateCheckSizeImageSrc(state, action) {
      return {
        ...state,
        imageCheckModalVisble: true,
        checkSizeImageSrc: 'data:image/jpeg;base64,' + action.payload[0],
        inputSize: action.payload[1],
        checkSize: action.payload[2],
        checkInfo: action.payload[3],
        checkError: action.payload[4],
      };
    },
    updateCountWidthHeightImage(state, action) {
      return {
        ...state,
        imageShowModalVisble: true,
        countWidthHeightImageSrc: 'data:image/jpeg;base64,' + action.payload[0],
        inputSize: action.payload[1],
        boardSize: action.payload[2],
        showInfo: action.payload[3],
      };
    }
  }
}
